import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.stream.Collectors;

public class Main {

    static String half = "01,12,23,34,45,56,67,78,89";//半顺

    static String[] halfResult=new String[]{"000","002","003","004","005","006","007","008","009",
            "022","024","025","026","027","028","029",
            "033","035","036","037","038","039","044",
            "046","047","048","049","055","057","058",
            "059","066","068","069","077","079","088",
            "099","111","113","114","115","116","117",
            "118","119","133","135","136","137","138","139",
            "144","146","147","148","149","155","157","158",
            "159","166","168","169","177","179","188","199",
            "222","224","225","226","227","228","229","244",
            "246","247","248","249","255","257","258","259",
            "266","268","269","277","279","288","299","333",
            "335","336","337","338","339","355","357","358",
            "359","366","368","369","377","379","388","399",
            "444","446","447","448","449","466","468","469",
            "477","479","488","499","555","557","558","559",
            "577","579","588","599","666","668","669","688",
            "699","777","779","799","888","999"};

    static String[] zaLiuResult=new String[]{"001","011","012","013","014","015","016","017",
            "018","019","023","034","045","056","067","078","089","112","122","123",
            "124","125","126","127","128","129","134","145","156","167","178","189",
            "223","233","234","235","236","237","238","239","245","256","267","278",
            "289","334","344","345","346","347","348","349","356","367","378","389",
            "445","455","456","457","458","459","467","478","489","556","566","567",
            "568","569","578","589","667","677","678","679","689","778","788","789",
            "889","899"};

    public static void main(String[] args) {

        String[] result = killCodeWithZaLiu(half);

        // 打印结果

        for (int i = 0; i < result.length; i++) {

            if(i%10==0){
                System.out.println();
            }
            System.out.print(result[i]+"\t");


        }


        // killCode(half)
    }


    // 杀两码,并且刷半顺


    /**
     * codes
     *
     * @param codes 以逗号分割的多个号码
     */
    public static String[] killCodeWithHalf(String codes){
        return killCode(codes,halfResult);
    }


    // 杀两码,并且刷杂六


    /**
     * codes
     *
     * @param codes 以逗号分割的多个号码
     */
    public static String[] killCodeWithZaLiu(String codes){
        return killCode(codes,zaLiuResult);
    }


    // 杀两码,双色球


    /**
     * codes
     *
     * @param codes 以逗号分割的多个号码
     */
    public static String[] killCode(String codes,String[] filteredCodes) {

        Set<String> killCodeSet = new HashSet<>();

        if (codes == null || codes.length() < 1) {
            return new String[0];
        }


        String[] killCodes = null;

        if (codes.contains(",")) {
            killCodes = codes.split(",");
        } else {
            killCodes = new String[]{codes};
        }
        List<String> resultList = new ArrayList<>();
        for (int i = 0; i < filteredCodes.length; i++) {

            String leftPad = filteredCodes[i];

            if (!isNeedKill(killCodes, leftPad)) {

                //

                String temp = sortStr(leftPad);

                if (killCodeSet.contains(temp)) {
                    continue;

                }

                killCodeSet.add(temp);
                resultList.add(leftPad);

            }
        }

        // java8 字符串左填充


        return resultList.toArray(new String[resultList.size()]);
    }



    // 杀两码,双色球


    /**
     * codes
     *
     * @param codes 以逗号分割的多个号码
     */
    public static String[] killCode(String codes) {

        Set<String> killCodeSet = new HashSet<>();

        if (codes == null || codes.length() < 1) {
            return new String[0];
        }


        String[] killCodes = null;

        if (codes.contains(",")) {
            killCodes = codes.split(",");
        } else {
            killCodes = new String[]{codes};
        }
        List<String> resultList = new ArrayList<>();
        for (int i = 0; i <= 999; i++) {

            String leftPad = leftPad(i, 3);

            if (!isNeedKill(killCodes, leftPad)) {

                //

                String temp = sortStr(leftPad);

                if (killCodeSet.contains(temp)) {
                    continue;

                }

                killCodeSet.add(temp);
                resultList.add(leftPad);

            }
        }

        // java8 字符串左填充


        return resultList.toArray(new String[resultList.size()]);
    }


    public static String sortStr(String str) {
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        return new String(chars);
    }


    public static boolean isNeedKill(String[] killCodes, String code) {
        for (int i = 0; i < killCodes.length; i++) {

            String killCode = killCodes[i];

            if(killCode.length()<2){
                continue;
            }

            // 获取killCode的第一个数字
            char first = killCode.charAt(0);
            char second = killCode.charAt(1);


            if (first == second) {
                // 判断first在code中是否存在至少2次
                if (countSubStr(code, first) >= 2) {
                    return true;
                } else {
                    continue;
                }
            }

            //如果code中同时包含这两个数字，则需要杀两码
            if (code.indexOf(first) != -1 && code.indexOf(second) != -1) {
                return true;
            }
        }
        return false;
    }

    public static int countSubStr(String str, char substr) {

        int count = 0;

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == substr) {
                count++;
            }
        }
        return count;
    }

    public static String leftPad(int number, int len) {
        // 字符串左填充0
        return String.format("%0" + len + "d", number);
    }
}